package com.aaa.forum.services.impl;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.UserSettingMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.UserSetting;
import com.aaa.forum.services.IUserSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class UserSettingServiceImpl implements IUserSettingService {

    @Resource
    private UserSettingMapper userSettingMapper;

    @Override
    public void UserInit(Long userId) {
        UserSetting userSetting = new UserSetting();
        userSetting.setUserId(userId);
        userSetting.setCollectionState((byte) 0);
        userSetting.setSubscribeListState((byte) 0);
        userSetting.setFansListState((byte) 0);
        userSetting.setState((byte) 0);
        userSetting.setDeleteState((byte) 0);
        Date date = new Date();
        userSetting.setCreateTime(date);
        userSetting.setUpdateTime(date);
        int row = userSettingMapper.insert(userSetting);
        if (row != 1) {
            log.info(ResultCode.FAILED_CREATE.toString() + "userId =" + userId);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public UserSetting selectByUserId(Long userId) {
        if (userId == null || userId < 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return userSettingMapper.selectByPrimaryKey(userId);
    }
}
